﻿using OpenTK;
using OpenTK.Graphics;

namespace Chimp.Core
{
    public class UIButton : UIFrame
    {
        private static ChimpTexture2D ButImg;
        public Color4 NormalColor = new Color4(0.6f, 0.6f, 0.6f, 0.5f);
        public Color4 OverColor = new Color4(0.8f, 0.8f, 0.8f, 0.85f);
        public Color4 PushColor = new Color4(1.0f, 1.0f, 1.0f, 0.92f);

        public UIButton(float x, float y, float w, float h, string text, ChimpWidget p)
            : base(x, y, w, h, "-1", p)
        {
            Text = text;
            if (ButImg == null)
            {
                ButImg = new ChimpTexture2D("Res/UI/guibutton2.png");
            }
            Image = ButImg;
        }

        public Color4 LerpColor(Color4 c1, Color4 c2, float t)
        {
            return new Color4(c1.R + (c2.R - c1.R)*t, c1.G + (c2.G - c1.G)*t, c1.B + (c2.B - c1.B)*t,
                              c1.A + (c2.A - c1.A)*t);
        }

        public UIButton IsSceneButton()
        {
            Pos = Pos + Parent.Pos + (Size/2.0f);
            return this;
        }

        public override void OnSceneRender()
        {
            Color4 tc;
            //  base.OnRender();
            if (MousePush)
            {
                tc = PushColor;
            }
            else if (MouseOver)
            {
                tc = OverColor;
            }
            else
            {
                tc = NormalColor;
            }

            TargetColor = LerpColor(TargetColor, tc, 0.2f);
            ChimpDraw.BlendMode = ChimpBlend.Alpha;
            ChimpText.ChimpTextCache txt = ChimpDraw.CheckText(Text);
            ChimpDraw.Image(new Vector4(DrawPos.X - Size.X/2, DrawPos.Y - Size.Y/2, Size.X, Size.Y), DrawRotation,
                            DrawScale, new Vector4(TargetColor.R, TargetColor.G, TargetColor.B, Alpha), Image, false);
            ChimpDraw.Image(new Vector4(DrawPos.X - txt.Width/2, DrawPos.Y - txt.Height/2, txt.Width, txt.Height),
                            DrawRotation, DrawScale, new Vector4(1, 1, 1, 1), txt.Visual, true);
        }

        public override void OnRender()
        {
            Color4 tc;
            //  base.OnRender();
            if (MousePush)
            {
                tc = PushColor;
            }
            else if (MouseOver)
            {
                tc = OverColor;
            }
            else
            {
                tc = NormalColor;
            }

            TargetColor = LerpColor(TargetColor, tc, 0.2f);
            BeginDraw();
            DrawImage(Image, GlobalPos, Size, TargetColor);
            //     if (Text == null || Text == "")
            //   {
            //     Text = " ";
            //}
            ChimpDraw.Text(GlobalPos + (Size/2.0f) + ChimpDraw.TextAlign(Text), Text);
        }
    }
}